package com.young.design.pattern.future;

import java.util.concurrent.*;

class jdkFutureReal implements Callable<String>{
    @Override
    public String call() throws Exception {
        System.out.println(Thread.currentThread().getName()+"\t 开始处理业务,需要10秒");
        TimeUnit.SECONDS.sleep(10);
        return "real处理完成";
    }
}

/**
 * @ClassName : JdkFutureDemo
 * @Author : hyy
 * @Date: 2020-03-14
 */
public class JdkFutureDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                1,
                1,
                1L,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(1),
                Executors.defaultThreadFactory()
                , new ThreadPoolExecutor.DiscardPolicy());
        jdkFutureReal jdkFutureReal = new jdkFutureReal();
        FutureTask<String> futureTask = new FutureTask<>(jdkFutureReal);
        threadPoolExecutor.execute(futureTask);
        System.out.println("开始处理其他业务");
        TimeUnit.SECONDS.sleep(3);
        if(!futureTask.isDone()){
            System.out.println("futureTask 还没有执行完成");
        }
        System.out.println("其他业务处理完毕");
        String result = futureTask.get();
        System.out.println("futureTask 任务执行完成， 结果：\t"+result);
        threadPoolExecutor.shutdown();
        System.gc();
    }
}
